<template>
  <el-table
    :data="permisstionArr"
    style="width: 100%; margin-bottom: 20px"
    row-key="id"
    border
  >
    <el-table-column label="名称" prop="name"></el-table-column>
    <el-table-column label="权限" prop="code"></el-table-column>
    <el-table-column label="修改时间" prop="updateTime"></el-table-column>
    <el-table-column label="操作" prop="">
      <!-- row即为已有菜单对象按钮的对象数据 -->
      <template #="{ row, $index }">
        <el-button
          size="samll"
          type="primary"
          @click="addPermisstion(row)"
          :disabled="row.level == 4 ? true : false"
        >
          {{ row.level == 3 ? '添加功能' : '添加菜单' }}
        </el-button>
        <el-button
          size="samll"
          type="primary"
          @click="updatePermisstion(row)"
          :disabled="row.level == 1 ? true : false"
        >
          编辑
        </el-button>
        <el-popconfirm :title="`你确定要删除${row.name}吗？`" width="260px" @confirm="removeMenu(row.id)">
          <template #reference>
            <el-button
          size="samll"
          type="primary"
          @click=""
          :disabled="row.level == 1 ? true : false"
        >
          删除
        </el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <!-- 对话框组件 -->
  <el-dialog
    v-model="dialogVisible"
    :title="menuData.id ? '更新菜单' : '添加菜单'"
  >
    <!-- 表单组件 -->
    <el-form>
      <el-form-item label="名称">
        <el-input
          placegolder="请输入菜单名称"
          v-model="menuData.name"
        ></el-input>
      </el-form-item>
      <el-form-item label="权限">
        <el-input
          placegolder="请输入菜单权限"
          v-model="menuData.code"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
//获取菜单请求api
import { reqAllPermission, reqAddOrUpdateMenu,reqRemoveMenu } from '@/api/acl/menu'
//引入ts类型
import type {
  PermissionResponseData,
  PermissionList,
  Permission,
  MenuParams,
} from '@/api/acl/menu/type'
//存贮数据菜单
let permisstionArr = ref<PermissionList>([])
//控制对话框的显示与隐藏
let dialogVisible = ref<boolean>(false)
//携带的参数
let menuData = reactive<MenuParams>({
  code: 'string',
  level: 0,
  name: 'string',
  pid: 0,
})
//获取组件
onMounted(() => {
  getHaspermisstion()
})
// 获取菜单数据方法
const getHaspermisstion = async () => {
  let result: PermissionResponseData = await reqAllPermission()
  if (result.code == 200) {
    permisstionArr.value = result.data
  }
}
//添加菜单按钮回调
const addPermisstion = (row: Permission) => {
  Object.assign(menuData, {
    id: 0,
    code: 'string',
    level: 0,
    name: 'string',
    pid: 0,
  })
  dialogVisible.value = true
  menuData.level = row.level + 1
  menuData.pid = row.id as number
}
//更新菜单按钮回调
const updatePermisstion = (row: Permission) => {
  dialogVisible.value = true
  Object.assign(menuData, row)
}
//确定按钮回调
const save = async () => {
  let result: any = await reqAddOrUpdateMenu(menuData)
  if (result.code == 200) {
    dialogVisible.value = false
    ElMessage({
      type: 'success',
      message: menuData.id ? '更新成功' : '添加成功',
    })
    getHaspermisstion()
  }
}
//删除按钮回调
const removeMenu =async(id:number) =>{
 let result =  await   reqRemoveMenu(id);
 if (result.code == 200) {
    ElMessage({
      type: 'success',
      message:  '删除成功' ,
    })
    getHaspermisstion()
  }
}
</script>

<style lang="scss" scoped></style>
